package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    //根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息，不包含用户的角色
        if(user == null){
            return null;
        }
        Integer userId = user.getId();
        //根据用户ID查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色ID查询关联的权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer[] findSelected(Integer userId) {
        return userDao.findSelected(userId);
    }

    @Override
    public void editRoles(Integer userId,Integer[]roleIds) {
        //删除修改之前的关系
        userDao.deleteOld(userId);
        Map<String,Integer> map=new HashMap<>();
        //重新添加user、role关系
        for (Integer roleId : roleIds) {
            map.put("roleId",roleId);
            map.put("userId",userId);
            userDao.editRoles(map);
        }
    }

    @Override
    public LinkedHashSet<Menu> showMenusByUsername(String username) {
        Integer userId=userDao.findUserIdByUsername(username);
        Integer[]roleIds =userDao.findRoleIdsByUserId(userId);
        List<Menu> menuParent = new ArrayList<>();
        List<Menu>finalMenu=new ArrayList<>();
        if (roleIds!=null &&roleIds.length>0){
                menuParent=menuDao.findByRoleId(roleIds);
            finalMenu = this.selectOneByOne(menuParent);
        }
        LinkedHashSet<Menu> menuList = new LinkedHashSet<>(finalMenu);
        return menuList;
    }

    public List<Menu> selectOneByOne(List<Menu>menuParent){
        for (Menu menu : menuParent) {
            List<Menu>childMenu=menuDao.findByParentId(menu.getId());
            if (childMenu!=null&&childMenu.size()>0){
                menu.setChildren(childMenu);
                selectOneByOne(childMenu);
            }
        }
        return menuParent;
    }
}
